﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.CrmDemo.Win.Data;
using DevExpress.XtraGrid.Views.Layout;
using DevExpress.CrmDemo.Win.Helpers;
using System.Diagnostics;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Layout.ViewInfo;
using DevExpress.XtraGrid.Columns;

namespace DevExpress.CrmDemo.Win.Controls {
    public partial class OpportunityColumn : DevExpress.XtraEditors.XtraUserControl {
        public OpportunityColumn() {
            InitializeComponent();
            
        }

        public GridControl GridControl {
            get { return this.gridControl; }
        }

        public LayoutView LayoutView {
            get { return this.layoutView; }
        }

        private LayoutViewCard hoverCard;

        private LayoutViewCard HoverCard {
            get { return hoverCard; }
            set {
                if (hoverCard != value) {
                    int prevHoverRow = hoverCard == null ? GridControl.InvalidRowHandle : hoverCard.RowHandle;

                    hoverCard = value;

                    // undo the previous hover 
                    layoutView.RefreshRow(prevHoverRow);

                    // set the new hover
                    if (hoverCard != null)
                        layoutView.RefreshRow(hoverCard.RowHandle);
                }
            }
        }

        private void layoutView_MouseMove(object sender, MouseEventArgs e) {
            LayoutView view = sender as LayoutView;

            LayoutViewHitInfo info = view.CalcHitInfo(new Point(e.X, e.Y));

            HoverCard = info.HitCard;
        }

        private void layoutView_CustomDrawCardFieldValue(object sender, RowCellCustomDrawEventArgs e) {
            bool hovering = HoverCard != null && e.RowHandle == HoverCard.RowHandle;
            bool isSideBand = e.Column == colLayoutSpacer;

            if (isSideBand) {
                e.Appearance.BackColor = hovering ? Color.FromArgb(105, 111, 127) : Color.FromArgb(102, 102, 102);
            } else {
                e.Appearance.BackColor = hovering ? Color.FromArgb(68, 75, 95) : Color.FromArgb(77, 77, 77);
            }
        }

        private void layoutView_MouseLeave(object sender, EventArgs e) {
            HoverCard = null;
        }
    }
}
